package com.bolt.report.business.modules.dataset.entity;

import com.bolt.report.business.modules.datasource.entity.ReportDataSourceEntity;
import com.bolt.report.business.modules.field.entity.ReportDbFieldEntity;
import com.bolt.report.business.modules.param.entity.ReportDbParamEntity;
import com.bolt.report.definition.ApiMethod;
import com.bolt.report.definition.RequestType;
import com.bolt.support.spring.jpa.entity.PersistableEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2021-11-17 14:15:01
 **/
@Access(AccessType.FIELD)
@Entity
@Table(name = "bolt_report_dataset")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Getter
@Setter
@EqualsAndHashCode
public class ReportDataSetEntity extends PersistableEntity<String> {
    private static final long serialVersionUID = 1L;
    /**
     * id
     */
    @Id
    @GeneratedValue(generator = "hibernate-uuid")
    @GenericGenerator(name = "hibernate-uuid", strategy = "org.hibernate.id.UUIDGenerator")
    @EqualsAndHashCode.Include
    private String id;


    /**
     * 数据集编码
     */
    @Column(name = "db_code", length = 32)
    @EqualsAndHashCode.Include
    private String dbCode;

    /**
     * 数据集名字
     */
    @Column(name = "db_ch_name", length = 50)
    private String dbChName;

    /**
     * 数据源类型
     */
    @Column(name = "db_type", length = 32)
    private Integer dbType;

    /**
     * 数据库表名
     */
    @Column(name = "db_table_name", length = 32)
    private String dbTableName;

    /**
     * 动态查询SQL
     */
    @Column(name = "db_dyn_sql", length = 2147483647)
    private String dbDynSql;

    /**
     * 数据源KEY
     */
    @Column(name = "db_key", length = 32)
    private String dbKey;

    /**
     * 填报数据源
     */
    @Column(name = "tb_db_key", length = 32)
    private String tbDbKey;

    /**
     * 填报数据表
     */
    @Column(name = "tb_db_table_name", length = 32)
    private String tbDbTableName;



    /**
     * spring类数据源 ID
     */
    @Column(name = "spring_key", length = 32)
    private String springKey;

    /**
     * 是否是列表0否1是 默认0
     */
    @Column(name = "is_list", length = 10)
    private boolean list;

    /**
     * 是否作为分页,0:不分页，1:分页
     */
    @Column(name = "is_page", length = 10)
    private boolean page;

    /**
     * 数据源
     */
    @Column(name = "db_source_id", length = 36,insertable=false,updatable=false)
    private String dbSourceId;


    /**
     * json数据，直接解析json内容
     */
    @Column(name = "json_data", length = 65535)
    private String jsonData;

    /**
     * api转换器
     */
    @Column(name = "api_convert", length = 255)
    private String apiConvert;

    /**
     * 请求地址
     */
    @Column(name = "api_url", length = 255)
    private String apiUrl;

    /**
     * 请求方式
     */
    @Column(name = "api_method")
    @Enumerated(EnumType.ORDINAL)
    private ApiMethod apiMethod;
    /**
     * 传参方式
     */
    @Column(name = "api_request_type")
    @Enumerated(EnumType.ORDINAL)
    private RequestType apiRequestType;

    @ManyToOne
    @JoinColumn(name = "db_source_id")
    @JsonIgnore
    private ReportDataSourceEntity dataSourceEntity;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "report_db_id", nullable = false)
    @JsonIgnore
    private Set<ReportDbFieldEntity> fields = new HashSet<>();

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "report_db_id", nullable = false)
    @JsonIgnore
    private Set<ReportDbParamEntity> params = new HashSet<>();

}